#include <REGX52.H>
#include "LCD1602.h"
#include "DS18B20.h"
#include "OneWire.h"
#include "DS18B20.h"
#include "Timer0.h"
#include "Key.h"
#include "AT24C02.h"
#include "Buzzer.h"
#include "Delay.h"
void main()
{
	float T = 0.0;
	char Max_T=0;
	char MIni_T=0;
	unsigned char Anjian = 0;
	DS18B20_ConvertT();
	Max_T=AT24C02_ReadByte(0);
	MIni_T=AT24C02_ReadByte(1);
	LCD_Init();
	LCD_ShowString(2,1,"Max");
	LCD_ShowString(2,7,"Min");
	Timer0Init();
	while(1)
	{
		DS18B20_ConvertT();
		T=DS18B20_RadT();
		Anjian=Key_FanHui();
		if(T<0)
		{
			LCD_ShowChar(1,1,'-');
			T=-T;
		}
		else if(T>0)
		{
			LCD_ShowChar(1,1,'+');
		}
		//LCD只能显示整数浮点数自动被强制类型转换成int丢失小数部分
		LCD_ShowNum(1,2,T,3);
		LCD_ShowChar(1,5,'.');
		LCD_ShowChar(1,10,0xDF);
		LCD_ShowChar(1,11,'C');
		//显示小数部分
		LCD_ShowNum(1,6,((unsigned long)(T*10000))%10000,4);
		if(Anjian)
		{
			if(Anjian==1)
			{
				Max_T++;
			}
			if(Anjian==2)
			{
				Max_T--;
				if(Max_T<MIni_T)
				{
					Max_T++;
				}
			}
			if(Anjian==3)
			{
				MIni_T++;
				if(Max_T<MIni_T)
				{
					MIni_T--;
				}
			}
			if(Anjian==4)
			{
				MIni_T--;
			}
		}
		LCD_ShowNum(2,4,Max_T,3);
		LCD_ShowNum(2,10,MIni_T,3);
		if(T>Max_T)
		{
			LCD_ShowString(1,14,"Max");
			Buzzer_Time(100);
		}
		if(T<MIni_T)
		{
			LCD_ShowString(1,14,"Min");
			Buzzer_Time(100);
		}
		if(T<Max_T&&T>MIni_T)
		{
			LCD_ShowString(1,14,"   ");
		}
		AT24C02_WriteByte(0,Max_T);
		Delay(5);
		AT24C02_WriteByte(1,MIni_T);
		Delay(5);
	}
}

void Int0_Rountine(void) interrupt 1
{
	static unsigned int a=0;
	TH0=(65535-1000)/256;
	TL0=(65535-1000)%256+1;
	
	a++;
	if(a>=20)
	{
		a=0;
		Key_DingShiQi();
	}
}